package com.trade.core.utils;

import com.trade.core.exception.TradeBaseException;
import com.trade.core.message.TradeBaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;

/**
 * 一些公共工具类
 * @author likb
 */
@Slf4j
public final class TradeCommonUtils {

    private TradeCommonUtils(){
    }

    /**
     * 日志
     */
    private static final Logger LOG = LoggerFactory.getLogger(TradeCommonUtils.class);

    /**
     * 调用rpc接口时，处理响应结果
     * @param response 响应结果
     */
    public static void dealResponse(TradeBaseResponse response)
    {
        if(response ==null){
            return;
        }

        if(response.getStatus() != HttpStatus.OK.value() && response.getStatus() != 40100)
        {
            LOG.error(response.getMessage());
            throw new TradeBaseException(response.getMessage(),response.getStatus());
        }
    }


    /**
     * 判断请求返回值是不是200
     * @param response 响应结果
     */
    public static boolean isSuccess(TradeBaseResponse response)
    {
        if(response == null){
            return false;
        }

        return response.getStatus() == HttpStatus.OK.value();
    }

}
